﻿Public Class FABM_Usuarios

    Dim sa As Boolean = False
    Dim modificar As Boolean

    Public sql As New MySQL

    Private Sub Cargar_combo(ByVal Combo As System.Object, ByVal Texto As String, ByVal Clave As String)
        Combo.Items.Add(New MiComboBox(Texto, Clave))
    End Sub

    Private Sub ABM_Usuarios_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If sa = False Then
            sa = True
            empezar()
        End If
    End Sub

    Private Sub empezar()
        Dim Dt, Dt1, Dt2, Dt3 As System.Data.DataTable
        cboxActividad.Items.Clear()
        CboxArea.Items.Clear()
        CboxSubArea.Items.Clear()
        btNuevo.Enabled = True
        btModificar.Enabled = True
        bteliminar.Enabled = True
        btAceptar.Enabled = False
        btCancelar.Enabled = False
        PEdicion.Visible = False
        DataGridView1.Enabled = True
        txtApellido.Clear()
        txtLogin.Clear()
        txtPass.Clear()
        txtApellido.Focus()


        Dt1 = sql.LEER("SELECT NombreApellido, login, clave, area, a.Id_area, subarea,sa.Id_subarea, actividad, ac.Id_actividad, u.Id_usuario FROM Usuarios u Left Join areas a on (u.id_area = a.id_area) Left Join subarea sa on (u.id_subarea = sa.id_subarea) Left Join actividad ac on (u.id_actividad = ac.id_actividad) ")
        DataGridView1.Rows.Clear()
        DataGridView1.Rows.Add(Dt1.Rows.Count)
        For i As Integer = 0 To Dt1.Rows.Count - 1
            DataGridView1.Item(0, i).Value = Dt1.Rows(i).Item("NombreApellido")
            DataGridView1.Item(1, i).Value = Dt1.Rows(i).Item("login")
            DataGridView1.Item(2, i).Value = Dt1.Rows(i).Item("clave")
            DataGridView1.Item(3, i).Value = Dt1.Rows(i).Item("actividad")
            DataGridView1.Item(4, i).Value = Dt1.Rows(i).Item("area")
            DataGridView1.Item(5, i).Value = Dt1.Rows(i).Item("subarea")
            DataGridView1.Item(6, i).Value = Dt1.Rows(i).Item("Id_actividad")
            DataGridView1.Item(7, i).Value = Dt1.Rows(i).Item("Id_area")
            DataGridView1.Item(8, i).Value = Dt1.Rows(i).Item("Id_subarea")
            DataGridView1.Item(9, i).Value = Dt1.Rows(i).Item("Id_usuario")
        Next
        Dt = sql.LEER("SELECT * FROM actividad")
        For I As Integer = 0 To Dt.Rows.Count - 1
            If Dt.Rows.Count <> 0 Then
                Cargar_combo(cboxActividad, Dt.Rows(I).Item("actividad").ToString(), Dt.Rows(I).Item("id_actividad").ToString())
            End If
        Next I

        Dt2 = sql.LEER("SELECT * FROM areas")
        For I As Integer = 0 To Dt2.Rows.Count - 1
            If Dt2.Rows.Count <> 0 Then
                Cargar_combo(CboxArea, Dt2.Rows(I).Item("area").ToString(), Dt2.Rows(I).Item("id_area").ToString())
            End If
        Next I

        Dt3 = sql.LEER("SELECT * FROM subarea")
        For I As Integer = 0 To Dt3.Rows.Count - 1
            If Dt3.Rows.Count <> 0 Then
                Cargar_combo(CboxSubArea, Dt3.Rows(I).Item("subarea").ToString(), Dt3.Rows(I).Item("id_subarea").ToString())
            End If
        Next I

    End Sub

    Private Sub btAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAceptar.Click
        Dim id_act As Integer
        Dim id_area As Integer
        Dim id_sub As Integer
        Dim id As Integer

        id_act = cboxActividad.Items(cboxActividad.SelectedIndex).ItemData()
        id_area = CboxArea.Items(CboxArea.SelectedIndex).ItemData()

        If CboxSubArea.Text = "" Then
            id_sub = -1
        Else
            id_sub = CboxSubArea.Items(CboxSubArea.SelectedIndex).ItemData()
        End If


        If modificar = True Then
            sql.INSERT_UPDATE_DELETE("Update usuarios SET NombreApellido='" & (txtApellido.Text) & "', login='" & (txtLogin.Text) & "', clave ='" & (txtPass.Text) & "',id_actividad='" & (id_act) & "',Id_area='" & (id_area) & "',Id_subarea='" & (id_sub) & "' WHERE Id_usuario = " & (DataGridView1.CurrentRow.Cells.Item(9).Value) & "")
            MsgBox("los datos se modificaron correctamente")
        Else
            PEdicion.Visible = True
            sql.INSERT_UPDATE_DELETE("Insert into usuarios(NombreApellido,login,clave,id_actividad,Id_area,Id_subarea) values('" & (txtApellido.Text) & "','" & (txtLogin.Text) & "','" & (txtPass.Text) & "','" & (id_act) & "','" & (id_area) & "','" & (id_sub) & "')")
            MsgBox("Los Datos se cargaron con exito")
        End If
        empezar()
        btSalir.Enabled = True
        modificar = False
    End Sub

    Private Sub btNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNuevo.Click
        btNuevo.Enabled = False
        btModificar.Enabled = False
        bteliminar.Enabled = False
        btSalir.Enabled = False
        btAceptar.Enabled = True
        btCancelar.Enabled = True
        DataGridView1.Enabled = False
        PEdicion.Visible = True



    End Sub

    Private Sub btbuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim Dt As System.Data.DataTable
        'Dim y As Integer

        'Dt = sql.LEER("SELECT u.*, na.nivel_acceso FROM usuarios u left join niveles_acceso na on (u.id_nivel = na.id_nivel_acceso) where apellido = '" & txtApellido.Text & "' ")

        'For I As Integer = 0 To Dt.Rows.Count - 1
        '    If Dt.Rows.Count <> 0 Then
        '        DataGridView1.Rows.Add(1)
        '        y = DataGridView1.Rows.Count - 2
        '        DataGridView1.Item(0, y).Value = Dt.Rows(I).Item("apellido")
        '        DataGridView1.Item(1, y).Value = Dt.Rows(I).Item("nombre")
        '        DataGridView1.Item(2, y).Value = Dt.Rows(I).Item("login")
        '        DataGridView1.Item(3, y).Value = Dt.Rows(I).Item("password")
        '        DataGridView1.Item(4, y).Value = Dt.Rows(I).Item("nivel_acceso")
        '    End If
        'Next I
    End Sub

    Private Sub btSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSalir.Click
        Me.Close()
    End Sub

    Private Sub btModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btModificar.Click
        btNuevo.Enabled = False
        btModificar.Enabled = False
        bteliminar.Enabled = False
        btSalir.Enabled = False
        btAceptar.Enabled = True
        btCancelar.Enabled = True
        DataGridView1.Enabled = False
        PEdicion.Visible = True
        txtApellido.Text = DataGridView1.CurrentRow.Cells.Item(0).Value
        txtLogin.Text = DataGridView1.CurrentRow.Cells.Item(1).Value
        txtPass.Text = DataGridView1.CurrentRow.Cells.Item(2).Value
        cboxActividad.SelectedIndex = DataGridView1.CurrentRow.Cells.Item(6).Value - 1
        CboxArea.SelectedIndex = DataGridView1.CurrentRow.Cells.Item(7).Value - 1

        If DataGridView1.CurrentRow.Cells.Item(8).Value.ToString = "" Then
            CboxSubArea.Text = ""
            CboxSubArea.Visible = False
            LblSubArea.Visible = False
        Else
            CboxSubArea.SelectedIndex = DataGridView1.CurrentRow.Cells.Item(8).Value - 1
        End If

        modificar = True

    End Sub

    Private Sub btCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancelar.Click
        DataGridView1.Enabled = True
        PEdicion.Visible = False
        btNuevo.Enabled = True
        btModificar.Enabled = True
        bteliminar.Enabled = True
        btAceptar.Enabled = False
        btCancelar.Enabled = False
        btSalir.Enabled = True
        empezar()

    End Sub

    Private Sub bteliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bteliminar.Click
        Dim id As Integer
        Dim response As MsgBoxResult

        response = MsgBox("desea eliminar el registro seleccionado?", MsgBoxStyle.YesNo)
        If response = MsgBoxResult.Yes Then
            id = DataGridView1.CurrentRow.Cells.Item(9).Value
            sql.INSERT_UPDATE_DELETE("Delete From usuarios WHERE Id_usuario= " & id & "")
            empezar()
        End If

    End Sub

    Private Sub CboxArea_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboxArea.SelectedIndexChanged
        If CboxArea.SelectedIndex = 0 Then
            CboxSubArea.Visible = True
            LblSubArea.Visible = True
        Else
            CboxSubArea.Visible = False
            LblSubArea.Visible = False
            CboxSubArea.Text = ""
        End If
    End Sub

    Private Sub CboxSubArea_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboxSubArea.SelectedIndexChanged
        If cboxActividad.SelectedIndex = 0 And CboxSubArea.SelectedIndex = 0 Then
            MsgBox("Un Jefe no puede ser gerente al mismo tiempo, elija otra subarea")
            btAceptar.Enabled = False
        Else
            btAceptar.Enabled = True
        End If

    End Sub
End Class